package com.wzx.mvc.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Parameter;

public class ParameterResolver {

    public static Object[] resolver(HandlerMethod hm, HttpServletRequest req, HttpServletResponse resp) {
        Object[] objects = new Object[hm.getMethod().getParameterCount()];

        Parameter[] parameters = hm.getMethod().getParameters();
        for (int i = 0; i < parameters.length; i++) {
            if (parameters[i].getType().isAssignableFrom(HttpServletRequest.class)) {
                objects[i] = req;
            }
            if (parameters[i].getType().isAssignableFrom(HttpServletResponse.class)) {
                objects[i] = resp;
            }
            if (parameters[i].getType().isAssignableFrom(String.class)) {
                objects[i] = req.getParameter(parameters[i].getName());
            }
        }
        return objects;
    }
}
